const util = require('../../../../utils/util.js');
const api = require('../../../../config/api.js');
const app = getApp();
Page({
  data: {
    pageNum: 1,
    interestJoinList:'',
  },
  onLoad: function(){
    var _this = this
    _this.setData({
      logininfo: app.globalData.logininfo,
      openid: app.globalData.logininfo.openid
    });
    console.log(_this.data.logininfo)
    var openid = app.globalData.logininfo.openid;
    var data = {};
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.selectOpenid = openid;
        data.access_token = res.access_token;
        data.pageNum = 1;
        data.pageSize = 10;
        util.post(api.Home_ParticipateCircleApiUrl, data).then(function (res2) {
          if (res2.code == 200) {
            var arr = []
            for (var i = 0; i < res2.result.list.length;i++){
              if (res2.result.list[i].identityType==1){
                arr.push(res2.result.list[i])
              }
            }
            _this.setData({
              interestJoinList: arr,
              pageNum: 1
            })
            console.log(arr)
          }
        })
      });
    }
  },
  /**
   * 进入圈子详情页面
   */
  navigateToCircleInfo: function (e) {
    var _this = this;
    var circleid = e.currentTarget.dataset.circleid;
    var openid = app.globalData.logininfo.openid;
    var data = {};
    data.circleid = circleid;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        _this.userIsCircleJoinQuery(api.Home_UserIsCircleJoinApiUrl, data);
      })
    } else {
      _this.userIsCircleJoinQuery(api.Home_UserIsCircleJoinUrl, data);
    }
  },
  /**
   * 校验用户是否为已加入的圈子：请求
   */
  userIsCircleJoinQuery: function (url, data) {
    var _this = this;
    util.post(url, data).then(function (res) {
      var userIsCircleJoin = false;
      if (res.code == 200) {
        userIsCircleJoin = res.result.userIsCircleJoin;
      }
      if (userIsCircleJoin) {
        // 已加入
        wx.navigateTo({
          url: '../../../../ucenter/pages/ucenter/personcenter/editdetails/circleinteraction/circleinteraction?circleid=' + data.circleid,
        })
      } else {
        // 未加入
        wx.navigateTo({
          url: '../../../../ucenter/pages/ucenter/personcenter/editdetails/circledetails/circledetails?circleid=' + data.circleid,
        })
      }
    });
  },
  showPop: function (e) {
    var _this = this
    var circleid = e.currentTarget.dataset.circleid;
    var index = e.currentTarget.dataset.index;
    var arr = _this.data.interestJoinList
    if (arr[index].circleidIsPlacedtop == 0) {
      _this.setData({
        popText: "置顶"
      })
    } else {
      _this.setData({
        popText: "取消置顶"
      })
    }
    var info = {}
    info.circleid = circleid
    info.index = index
    _this.setData({
      funShow: true,
      info: info
    })
  },
  onClose: function () {
    var _this = this
    _this.setData({
      funShow: false
    })
  },
  zhiding: function(){
    var _this = this;
    var info = _this.data.info;
    var arr = _this.data.interestJoinList
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.openid = openid;
        data.access_token = res.access_token;
        data.id = arr[info.index].id;
        util.post(api.Home_UpdateCircleidIsPlacedtopApiUrl, data).then(function (res) {
          if (res.code == 200) {
            _this.onLoad();
            _this.onClose();
          }
        })
      })
    }
  },
  //新建圈子
  doPublish: function () {
    wx.navigateTo({
      url: '../../../../ucenter/pages/ucenter/personcenter/publish/publish',
    })
  },
})